<template>
  <el-dialog
    title="修改邮件配置"
    :visible.sync="dialogVisible"
    :show="show"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    :before-close="handleClose"
    width="500px"
    @open="open"
  >
    <el-form ref="form" v-loading="infoLoading" :model="info" :rules="rules" label-width="140px">
      <el-scrollbar ref="myScrollbar" wrap-class="wrapClass" class="form-scrollbar">
        <div class="el-dialog-div">
          <el-form-item label="邮件服务平台">
            <el-radio-group v-model="info.emailPlatform">
              <el-radio :label="0">mailjet</el-radio>
              <el-radio :label="1">rushmail</el-radio>
            </el-radio-group>
          </el-form-item>
          <template v-if="info.emailPlatform === 0">
            <el-form-item label="mailjet独立使用" prop="useIndependentMailjet">
              <el-radio-group v-model="info.useIndependentMailjet">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <template v-if="info.useIndependentMailjet">
              <el-form-item label="mailjet apikey" prop="mailjetApiKey">
                <el-input v-model="info.mailjetApiKey" placeholder="apikey"/>
              </el-form-item>
              <el-form-item label="mailjet apisecret" prop="mailjetApiSecret">
                <el-input v-model="info.mailjetApiSecret" placeholder="apisecret"/>
              </el-form-item>
            </template>
          </template>

          <template v-if="info.emailPlatform === 1">
            <el-form-item label="rushmail独立使用" prop="useIndependentRushmail">
              <el-radio-group v-model="info.useIndependentRushmail">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <template v-if="info.useIndependentRushmail">
              <el-form-item label="rushmail customId" prop="rushmailCustomId">
                <el-input v-model="info.rushmailCustomId" placeholder="customId"/>
              </el-form-item>
              <el-form-item label="rushmail secretKey" prop="rushmailSecretKey">
                <el-input v-model="info.rushmailSecretKey" placeholder="secretKey"/>
              </el-form-item>
            </template>
          </template>
        </div>
      </el-scrollbar>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-row>
        <el-col :span="12" class="btnEdit">
          <el-button :disabled="infoLoading" type="primary" @click="subInfo">确 定</el-button>
        </el-col>
        <el-col :span="12" class="btnEdit">
          <el-button @click="handleClose">取 消</el-button>
        </el-col>
      </el-row>
    </div>
  </el-dialog>
</template>

<style scoped>

/deep/ .el-dialog__body {
  padding: 0;
}

.form-scrollbar {
  height: 500px;
}

.el-dialog-div {
  padding: 20px 20px 0 20px;
  min-width: 400px;
}

.avatar-uploader {
  width: 178px;
  height: 178px;
  border: 1px dashed #d9d9d9;
  border-radius: 10px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background-color: #fbfdff;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<script>
import { postAction } from '@/api/common'
export default {
  name: 'editEmailSettings',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: {
      required: true,
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: this.show,
      isChange: false,
      initForm: true,
      // 页面元素
      info: {
        emailPlatform: 0,
        useIndependentMailjet: 0,
        mailjetApiKey: 0,
        mailjetApiSecret: 0,
        useIndependentRushmail: 0,
        rushmailCustomId: 0,
        rushmailSecretKey: 0
      },
      oemInfo: undefined,
      infoLoading: true,

      // 验证
      rules: {
      }
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show
      this.initForm = true
    },
    info: {
      handler: function() {
        if (this.initForm) {
          this.isChange = false
          this.initForm = false
        } else {
          this.isChange = true
        }
      },
      deep: true
    }
  },

  methods: {
    async open() {
      this.infoLoading = true
      // eslint-disable-next-line eqeqeq
      const { data } = await postAction({ id: this.id }, 'oem/GetInfo')
      this.info = data.emailSettings
      console.log(this.info)

      this.infoLoading = false
    },
    subInfo() {
      this.$refs.form.validate(async(valid) => {
        // form验证
        if (valid) {
          this.infoLoading = true

          // eslint-disable-next-line eqeqeq
          const info = this.info
          await postAction(info, 'oem/EditEmailSettings')

          this.infoLoading = false
          // 调用父级页面方法
          this.closeAction()
          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 2000
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose(done) {
      if (this.isChange) {
        this.$confirm('您有未保存的修改，确定关闭此页吗？', '提示', {
          type: 'warning'
        })
          .then(_ => {
            this.$emit('update:show', false)
          })
          .catch(e => {
            console.log(e)
          })
      } else {
        this.$emit('update:show', false)
      }
    },
    closeAction() {
      this.$emit('closeAction')
    }

  }
}
</script>
